<?php

namespace app\company\controller\statistics\home;

use app\BaseController;
use app\model\AreasModel;
use app\model\InterfereUserModel;
use app\model\UnitModel;
use app\model\UserModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 辖区数据
     * @return bool|string
     */
    public function areas(): bool|string
    {
        try {
            $AreasModel = new AreasModel();
            if ($this->userData['type'] == 1) {
                $data = $AreasModel->field('id,name')
                    ->where('pid', '=', 0)->withCount([
                        'provinceAllUnit' => function ($query) {
                            $query->where('company_id','=',$this->userData['company_id']);
                        },
                        'provinceAllUser' => function ($query) {
                            $query->where('company_id','=',$this->userData['company_id']);
                        }
                    ])->select()->toArray();
            } else {
                switch ($this->userData['level']) {
                    case 1:
                        if (in_array($this->userData['province_id'], [
                            110000,
                            120000,
                            500000,
                            310000
                        ])) {
                            $data = $AreasModel->field('id,name')
                                ->where('top_layer_id', '=', $this->userData['province_id'])->withCount([
                                    'cityAllUnit' => function ($query) {
                                        $query->where('company_id','=',$this->userData['company_id']);
                                    },
                                    'cityAllUser' => function ($query) {
                                        $query->where('company_id','=',$this->userData['company_id']);
                                    }
                                ])->select()->toArray();
                        } else {
                            $data = $AreasModel->field('id,name')
                                ->where('pid', '=', $this->userData['province_id'])->withCount([
                                    'cityAllUnit' => function ($query) {
                                        $query->where('company_id','=',$this->userData['company_id']);
                                    },
                                    'cityAllUser' => function ($query) {
                                        $query->where('company_id','=',$this->userData['company_id']);
                                    }
                                ])->select()->toArray();
                        }
                        $code = $this->userData['province_id'];
                        break;
                    case 2:
                        $data = $AreasModel->field('id,name')
                            ->where('pid', '=', $this->userData['city_id'])->withCount([
                                'areaAllUnit' => function ($query) {
                                    $query->where('company_id','=',$this->userData['company_id']);
                                },
                                'areaAllUser' => function ($query) {
                                    $query->where('company_id','=',$this->userData['company_id']);
                                }
                            ])->select()->toArray();
                        $code = $this->userData['city_id'];
                        break;
                    case 3:
                        $data = $AreasModel->field('id,name')
                            ->where('id', '=', $this->userData['area_id'])->withCount([
                                'areaAllUser' => function ($query) {
                                    $query->where('company_id','=',$this->userData['company_id']);
                                }
                            ])->select()->toArray();
                        $code = $this->userData['area_id'];
                        break;
                    default:
                        throw new \Exception('该角色没有该功能！');
                }
            }
            foreach ($data as &$item){
                if (!empty($item['city_all_unit_count'])){
                    $item['all_unit_count'] = $item['city_all_unit_count'];
                }
                if (!empty($item['city_all_user_count'])){
                    $item['all_user_count'] = $item['city_all_user_count'];
                }
                if (!empty($item['area_all_unit_count'])){
                    $item['all_unit_count'] = $item['area_all_unit_count'];
                }
                if (!empty($item['area_all_user_count'])){
                    $item['all_user_count'] = $item['area_all_user_count'];
                }
                if (!empty($item['province_all_unit_count'])){
                    $item['all_unit_count'] = $item['province_all_unit_count'];
                }
                if (!empty($item['province_all_user_count'])){
                    $item['all_user_count'] = $item['province_all_user_count'];
                }
            }
            return success([
                'code' => $code??'',
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 主页数据统计
     * @return bool|string
     */
    public function Index(): bool|string
    {
        try {
            $data = [];
            $UnitModel = new UnitModel();
            $UserModel = new UserModel();
            $data['unit_province_count'] = $UnitModel->where([
                ['id', 'in', $this->userData['subset_unit']],
                ['level', '=', 1]
            ])->count();
            $data['unit_city_count'] = $UnitModel->where([
                ['id', 'in', $this->userData['subset_unit']],
                ['level', '=', 2]
            ])->count();
            $data['unit_area_count'] = $UnitModel->where([
                ['id', 'in', $this->userData['subset_unit']],
                ['level', '=', 3]
            ])->count();
            $data['unit_count'] = $data['unit_province_count'] + $data['unit_city_count'] + $data['unit_area_count'];
            $data['user_count'] = $UserModel->where([
                ['unit_id', 'in', $this->userData['subset_unit']],
                ['role_mode', '=', 3],
            ])->count();
            $data['user_boy_count'] = $UserModel->where([
                ['unit_id', 'in', $this->userData['subset_unit']],
                ['role_mode', '=', 3],
                ['sex', '=', 1]
            ])->count();
            $data['user_girl_count'] = $data['user_count'] - $data['user_boy_count'];

            $InterfereUserModel = new InterfereUserModel();
            $data['interfere_user_count'] = $InterfereUserModel->where('unit_id', 'in', $this->userData['subset_unit'])->distinct(true)->count('uid');
            $uid = $InterfereUserModel->where([
                ['unit_id', 'in', $this->userData['subset_unit']],
                ['status', '=', 0]
            ])->column('uid');
            $data['in_interfere_user_count'] = count($uid);
            $data['in_interfere_user_boy'] = $UserModel->where([
                ['id', 'in', $uid],
                ['sex', '=', 1]
            ])->count();
            $data['in_interfere_user_girl'] = $data['in_interfere_user_count'] - $data['in_interfere_user_boy'];

            $uid2 = $InterfereUserModel->where([
                ['unit_id', 'in', $this->userData['subset_unit']],
                ['status', '=', 1]
            ])->column('uid');
            $data['end_interfere_user_count'] = count($uid2);
            $data['end_interfere_user_boy'] = $UserModel->where([
                ['id', 'in', $uid2],
                ['sex', '=', 1]
            ])->count();
            $data['end_interfere_user_girl'] = $data['end_interfere_user_count'] - $data['end_interfere_user_boy'];
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


}